package com.ply.flashsalessystem.service.impl;

import com.ply.flashsalessystem.entity.pojo.ReturnOfGoods;
import com.ply.flashsalessystem.entity.pojo.ReturnOfGoodsDetails;
import com.ply.flashsalessystem.mapper.ReturnOfGoodsMapper;
import com.ply.flashsalessystem.service.OrderForGoodsDetaileService;
import com.ply.flashsalessystem.service.ReturnOfGoodsDetailsService;
import com.ply.flashsalessystem.service.ReturnOfGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XiaoPan
 * @since 2022-03-23
 */
@Service
public class ReturnOfGoodsServiceImpl extends ServiceImpl<ReturnOfGoodsMapper, ReturnOfGoods> implements ReturnOfGoodsService {

    @Autowired
    ReturnOfGoodsMapper returnOfGoodsMapper;

    @Autowired
    ReturnOfGoodsDetailsService returnOfGoodsDetailsService;

    @Autowired
    OrderForGoodsDetaileService orderForGoodsDetaileService;
    /**
     * 申请退单
     * @param orgId 订单id
     * @param orgDId 订单详情id
     * @param price 商品单价
     * @param nums 申请退单数量
     * @param message 退单留言
     */

    @Override
    public boolean requestReturnOrder(Integer orgId, Integer orgDId,double price, double nums, String message) {
        ReturnOfGoods returnOfGoods = new ReturnOfGoods();
        returnOfGoods.setOrgId(orgId);
        returnOfGoodsMapper.insert(returnOfGoods);


        //新增 退单详情表
        ReturnOfGoodsDetails returnOfGoodsDetails = new ReturnOfGoodsDetails();
        returnOfGoodsDetails.setRogId(returnOfGoods.getRogId());
        returnOfGoodsDetails.setOrgDId(orgDId);
        returnOfGoodsDetails.setNums(nums);
        returnOfGoodsDetails.setPrice(price);
        returnOfGoodsDetails.setAmount(price * nums);
        returnOfGoodsDetails.setMessage(message);
        returnOfGoodsDetailsService.save(returnOfGoodsDetails);
        return true;
    }

    /**
     * 一次性增加多个 退单详情
     * @param orgId
     * @param list
     */
    @Override
    public void requestReturnOrderMan(Integer orgId, List<ReturnOfGoodsDetails> list) {
        ReturnOfGoods returnOfGoods = new ReturnOfGoods();
        returnOfGoods.setOrgId(orgId);
        returnOfGoodsMapper.insert(returnOfGoods);

        //一次性增加 多条 退单详情表
        for (ReturnOfGoodsDetails returnOfGoodsDetails : list) {
            returnOfGoodsDetails.setRogId(returnOfGoods.getRogId());
        }
        returnOfGoodsDetailsService.saveBatch(list);
    }



}
